package com.jsz.peini.server.thread;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jsz.peini.bean.search.SearchHot;
import com.jsz.peini.bean.search.SearchHotExample;
import com.jsz.peini.bean.search.SearchHotExample.Criteria;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.common.util.SpringContextUtil;
import com.jsz.peini.mapper.search.SearchHotMapper;

public class SearchHotThread extends Thread {
	private static final Logger logger = LoggerFactory.getLogger(SearchHotThread.class);
	public void init(){
		this.start();
	}
	
	@Override
	public void run() {
		super.run();
		
		SearchHotMapper searchHotMapper = (SearchHotMapper)SpringContextUtil.getBean(SearchHotMapper.class);
		
		while(true)
		{
			try {
				SearchHot searchHot = PeiniUtils.hotSearchList.take();
				//PeiniUtils.hotList.add(searchHot);
				
				//if(PeiniUtils.hotList.size() == 2){
					SearchHotExample example = new SearchHotExample();
					Criteria criteria = example.createCriteria();
					criteria.andHotNameEqualTo(searchHot.getHotName());
					criteria.andTypeEqualTo(searchHot.getType());
					example.setOrderByClause("id desc");
					List<SearchHot> list = searchHotMapper.selectByExample(example);
					if(list.size() == 0){
						searchHot.setHotNum(20);
						searchHotMapper.insert(searchHot);
					}else{
						searchHot = list.get(0);
						searchHot.setHotNum(searchHot.getHotNum()+20);
						searchHotMapper.updateByPrimaryKey(searchHot);
					}
					PeiniUtils.hotList.clear();
				//}
				
				Thread.sleep(1);
			} catch (Exception e) {
				logger.error(e.getMessage(),e);
			}
		}
		
	}
}
